#lang racket

(require "rename-mutable-pair-ops.rkt")

(provide make-queue empty-queue? front-queue insert-queue! delete-queue! print-queue)

(define (front-ptr queue) (car queue))

(define (rear-ptr queue) (cdr queue))

(define (set-front-ptr! queue item) (set-car! queue item))

(define (set-rear-ptr! queue item) (set-cdr! queue item))

;INTERFACE FUNCTIONS-----------------------------------------------------------------------------

;(define (empty-queue? queue) (null? (front-ptr queue)))

;(define (make-queue) (cons '() '()))

;(define (front-queue queue)
;  (if (empty-queue? queue)
;      (error "queue is empty: " queue)
;      (car (front-ptr queue))))

;(define (insert-queue! queue item)
;  (let ((new-pair (cons item '())))
;    (cond ((empty-queue? queue)
;           (set-front-ptr! queue new-pair)
;           (set-rear-ptr! queue new-pair)
;           queue)
;          (else
;           (set-cdr! (rear-ptr queue) new-pair)
;           (set-rear-ptr! queue new-pair)
;           queue))))

;(define (delete-queue! queue)
;  (cond ((empty-queue? queue)
;         (error "queue is empty: " queue))
;        (else
;         (set-front-ptr! queue (cdr (front-ptr queue)))
;         queue)))

;(define (print-queue queue) (print-list (front-ptr queue)))

(define (make-queue)
  (let ((front-ptr '())
        (rear-ptr '()))
    (define (empty-queue?) (null? front-ptr))
    (define (front-queue)
      (if (empty-queue?)
          (error "queue is emty")
          (car front-ptr)))
    (define (insert-queue! item)
      (let ((new-pair (cons item '())))
        (cond ((empty-queue?)
               (set! front-ptr new-pair)
               (set! rear-ptr new-pair)
               dispatch)
              (else
               (set-cdr! rear-ptr new-pair)
               (set! rear-ptr new-pair)
               dispatch))))
    (define (delete-queue!)
      (cond ((empty-queue?)
             (error "queue is empty"))
            (else
             (set! front-ptr (cdr front-ptr))
             dispatch)))
    (define (print-queue) (print-list front-ptr))
    (define (dispatch m)
      (cond ((eq? m 'empty-queue?) empty-queue?)
            ((eq? m 'front-queue) front-queue)
            ((eq? m 'insert-queue!) insert-queue!)
            ((eq? m 'delete-queue!) delete-queue!)
            ((eq? m 'print-queue) print-queue)))
    dispatch))

(define (empty-queue? queue) ((queue 'empty-queue?)))

(define (front-queue queue) ((queue 'front-queue)))

(define (insert-queue! queue item) ((queue 'insert-queue!) item))

(define (delete-queue! queue) ((queue 'delete-queue!)))

(define (print-queue queue) ((queue 'print-queue)))